import { _decorator, Node,Component, UITransform, Button, Label, EventTouch, Vec3, director, Canvas, Color, Sprite, Layers, sys, LabelOutline, instantiate } from 'cc';
import { ResourceManagerTest } from './ResourceManagerTest';
import { AudioManagerTest } from './AudioManagerTest';
import { SceneManagerTest } from './SceneManagerTest';
import { PrefabTest } from './PrefabTest';
import { PopupTest } from './PopupTest';
import { PoolManagerTest } from './PoolManagerTest';
import { CollisionTest } from './CollisionTest';
import { CommonUtils } from '../utils/CommonUtils';
import { GlobalManager } from '../global/GlobalManager';
import { GlobalRoot } from '../global/GlobalRoot';
import LoadScrollView from '../utils/LoadScrollView';
import { RVOTest } from './RVOTest';
const { ccclass } = _decorator;

@ccclass('GameFeatureTest')
export class GameFeatureTest extends Component {
    allNode:any =null;

    // ================== 测试模块与用例配置 ==================
    public _modules = [
        { name: '资源管理', method: 'runResourceManagerTest' },
        { name: '音频管理', method: 'runAudioManagerTest' },
        { name: 'Prefab', method: 'runPrefabTest' },
        { name: '弹窗', method: 'runPopupTest' },
        { name: '对象池', method: 'runPoolManagerTest' },
        { name: '碰撞检测', method: 'runCollisionTest' },
        { name: 'RVO智能体', method: 'runRVOTest' },
        { name: '场景管理', method: 'runSceneManagerTest' },
        { name: '全部', method: 'runAll' }
    ];
    public _moduleCases = {
        runResourceManagerTest: [
            { name: '全部', method: 'runAll' },
            { name: '本地单图', method: 'testLocalSingleImage' },
            { name: '本地合图', method: 'testLocalAtlasImage' },
            { name: 'Bundle单图', method: 'testBundleSingleImage' },
            { name: 'Bundle合图', method: 'testBundleAtlasImage' },
            { name: '本地合图序列帧', method: 'testLocalFramesAtlas' },
            { name: '本地散图序列帧', method: 'testLocalFramesScatter' },
            { name: '本地Spine', method: 'testLocalSpine' },
            { name: 'Bundle合图序列帧', method: 'testBundleFramesAtlas' },
            { name: 'Bundle散图序列帧', method: 'testBundleFramesScatter' },
            { name: '远程单图', method: 'testRemoteSingleImage' },
            { name: '远程散图序列帧', method: 'testRemoteFramesScatter' },
            { name: '远程Spine', method: 'testRemoteSpine' },
            { name: '预加载图片', method: 'testPreloadImg' },
            { name: '预加载序列帧', method: 'testPreloadFrames' },
            { name: '预加载Spine', method: 'testPreloadSpine' },
            { name: '预加载Prefab', method: 'testPreloadPrefab' },
            { name: '预加载Json', method: 'testPreloadJson' }
        ],
        runAudioManagerTest: [
            { name: '全部', method: 'runAll' },
            { name: '播放音效', method: 'testPlayEffect' },
            { name: '播放音乐', method: 'testPlayMusic' },
            { name: '停止所有', method: 'testStopAll' },
            { name: '音量调节', method: 'testSetVolume' }
        ],
        runPrefabTest: [
            { name: '全部', method: 'runAll' },
            { name: '加载Prefab', method: 'testLoadPrefab' },
            { name: '实例化Prefab', method: 'testInstantiatePrefab' },
            { name: '显示并销毁Prefab', method: 'testShowAndDestroyPrefab' }
        ],
        runPopupTest: [
            { name: '全部', method: 'runAll' },
            { name: '加载弹窗', method: 'testLoadPopup' },
            { name: '显示弹窗', method: 'testShowPopup' },
            { name: '关闭弹窗', method: 'testClosePopup' },
            { name: '弹窗缓存', method: 'testPopupCache' }
        ],
        runPoolManagerTest: [
            { name: '全部', method: 'runAll' },
            { name: '批量获取/回收', method: 'testBatchGetRelease' },
            { name: '池容量限制', method: 'testPoolLimit' },
            { name: '自动清理', method: 'testAutoClear' },
        ],
        runCollisionTest: [
            { name: '全部', method: 'runAll' },
            { name: '2D圆形碰撞', method: 'testCircleCircleWithNode' },
            { name: '2D矩形碰撞', method: 'testRectRect' },
            { name: '2D多边形碰撞', method: 'testPolygonPolygon' },
            { name: '2D批量圆碰撞', method: 'testBatchCircle' },
            { name: '3D球体碰撞', method: 'testSphereSphere' },
            { name: '3D盒体碰撞', method: 'testBoxBox' },
            { name: '3D三角形碰撞', method: 'testTriangleTriangle' },
            { name: '3D多面体碰撞', method: 'testPolyhedronPolyhedron' },
            { name: '3D批量球体碰撞', method: 'testBatchSphere' }
        ],
        runRVOTest: [
            { name: '2D批量', method: 'test2DBatch' },
            { name: '3D批量', method: 'test3DBatch' },
            { name: '2D动态', method: 'test2DDynamic' },
            { name: '3D动态', method: 'test3DDynamic' }
        ],
        runSceneManagerTest: [
            { name: '全部', method: 'runAll' },
            { name: '加载场景', method: 'testLoadScene' },
            { name: '加载Bundle场景', method: 'testLoadBundleScene' },
            { name: '切换回调', method: 'testOnLaunchedCallback' }
        ]
    };
    public _3dModules = [
        'runCollision3DTest',
    ];

    protected start(): void {
        this.allNode = CommonUtils.getAllChildrenMap(this.node);
        CommonUtils.add2DClick(this.allNode.closeBtn,()=>{
            GlobalRoot.instance.UiManager.closePopup(GlobalManager.popups.testPop)
        });
        this.allNode.ScrollView.getComponent(LoadScrollView).initLocalUi(this._modules, (parent,data) => {
           let item = instantiate(this.allNode.itemBtn);
           item.parent = parent;
           item.active = true;
           item.getChildByName("title").getComponent(Label).string = data.name;
           CommonUtils.add2DClick(item,()=>{
               this.openCase(data.method)
           })

        });
    }
    getTestClassByModuleMethod(method: string) {
        switch (method) {
            case 'runResourceManagerTest': return ResourceManagerTest;
            case 'runAudioManagerTest': return AudioManagerTest;
            case 'runPrefabTest': return PrefabTest;
            case 'runPopupTest': return PopupTest;
            case 'runPoolManagerTest': return PoolManagerTest;
            case 'runCollisionTest': return CollisionTest;
            case 'runRVOTest': return RVOTest;
            case 'runSceneManagerTest': return SceneManagerTest;
            default: return null;
        }
    }


    openCase(name: string) {
        CommonUtils.popUp(this.allNode.caseView);
        const parent2D = this.allNode.casebg;
        const parent3D = director.getScene();
        this.allNode.caseScrollView.getComponent(LoadScrollView).initLocalUi(this._moduleCases[name], (parent,data) => {
            let item = instantiate(this.allNode.itemBtn);
            item.parent = parent;
            item.active = true;
            item.getChildByName("title").getComponent(Label).string = data.name;
            CommonUtils.add2DClick(item, async () => {
                const testClass = this.getTestClassByModuleMethod(name);
                if (testClass && typeof testClass[data.method] === 'function') {
                    // @ts-ignore
                    const is3D = (this._3dModules as any[]).includes(name);
                    const useParent = is3D ? parent3D : parent2D;
                    if (useParent && useParent.removeAllChildren) useParent.removeAllChildren();
                    testClass[data.method](useParent)
                   
                   
                }
            });
        });
        CommonUtils.add2DClick(this.allNode.caseBlock, () => {
            this.allNode.caseView.active = false;
            this.allNode.casebg.removeAllChildren();
            // 可选：如果有casebg3D，也清理
            // this.allNode.casebg3D?.removeAllChildren();
        });
    }
  
    closeView(){
        GlobalRoot.instance.UiManager.closePopup(GlobalManager.popups.testPop)

    }



    // ================== 兼容原有一键全部测试API ==================
    public runAll() {
        this.runResourceManagerTest();
        this.runAudioManagerTest();
        this.runPrefabTest();
        this.runPopupTest();
        this.runPoolManagerTest();
        this.runCollisionTest();
        setTimeout(() => { this.runSceneManagerTest(); }, 5000);
    }
    public runResourceManagerTest() { ResourceManagerTest.runAll(); }
    public runAudioManagerTest() { AudioManagerTest.runAll(); }
    public runPrefabTest() { PrefabTest.runAll(); }
    public runPopupTest() { PopupTest.runAll(); }
    public runPoolManagerTest() { PoolManagerTest.runAll(); }
    public runCollisionTest() {
        // 依次调用所有可视化碰撞测试方法
        CollisionTest.testCircleCircle(this.allNode.casebg);
        CollisionTest.testRectRect(this.allNode.casebg);
        CollisionTest.testPolygonPolygon(this.allNode.casebg);
        CollisionTest.testBatchCircle(this.allNode.casebg);
        CollisionTest.testSphereSphere(this.allNode.casebg);
        CollisionTest.testBoxBox(this.allNode.casebg);
        CollisionTest.testTriangleTriangle(this.allNode.casebg);
        CollisionTest.testPolyhedronPolyhedron(this.allNode.casebg);
        CollisionTest.testBatchSphere(this.allNode.casebg);
    }
    public runRVOTest() { /* 可选：批量或动态测试入口 */ }
    public runSceneManagerTest() { SceneManagerTest.runAll(); }
}